package com.weiyuwqj.controller.user;

import com.weiyuwqj.api.service.SysTempcodeService;
import com.weiyuwqj.api.service.BasUserService;
import com.weiyuwqj.api.constant.Constants;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.model.SysTempcode;
import com.weiyuwqj.api.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * Created by zhangyong on 2016/11/24.
 * 忘记密码
 */
@Controller
@RequestMapping("/forgetPassword")
public class UserForgetPasswordController {

    @Autowired
    BasUserService userService;
    @Autowired
    SysTempcodeService tempcodeService;

    @RequestMapping("")
    public Object forgetPassword(){
        return "main/forget";
    }

    /**
     * 手机验证码校验
     * @param request
     * @param mobile  手机号码
     * @param mobileCode  手机验证码
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkMobile")
    public Object checkMobile(HttpServletRequest request,String mobile ,String mobileCode) {

        BasUser user = userService.getUserByMobile(mobile);
        if (user == null) {
            throw new CrowdException("user.login.username.no_such_user");
        }
        Long validPhoneExpiredTime = (Long) request.getSession().getAttribute("validPhoneExpiredTime");
        String validPhoneCode = request.getSession().getAttribute("validPhoneCode").toString();

        //发送短信
        Date now = new Date();
        long between = (now.getTime() - validPhoneExpiredTime) / 1000;

        if (!(validPhoneCode.equals(mobileCode))) {
            throw new CrowdException("user.register.randomCode.error");
        }
        if (between > Constants.VALIDATETIME) {
            throw new CrowdException("user.register.randomCode.timeout");
        }

        SysTempcode tempcode = tempcodeService.getByMobileAndCode(mobile, mobileCode);
        if (tempcode == null) {
            throw new CrowdException("user.register.randomCode.error");
        }

        return Message.OK;
    }


    /**
     * 忘记密码
     * @param password  密码
     * @param mobile  手机号码
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/doForgetPassword", method= RequestMethod.POST)
    public Object backPass(String password,String mobile) {

        BasUser user = userService.getUserByMobile(mobile);
        if (user == null) {
            throw new CrowdException("user.login.username.no_such_user");
        }

        try {
            password = MD5.toMD5(password + user.getSalt());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        userService.modifyPassword(mobile, password);
        return Message.OK;
    }


}
